package com.sleepingcat.flutter_boost_dart.flutter_boost_dart

import android.app.Application
import android.content.Intent
import com.idlefish.flutterboost.FlutterBoost
import com.idlefish.flutterboost.FlutterBoostDelegate
import com.idlefish.flutterboost.FlutterBoostRouteOptions
import com.idlefish.flutterboost.FlutterBoostSetupOptions
import com.idlefish.flutterboost.containers.FlutterBoostActivity
import com.sleepingcat.flutter_boost_dart.pigeons.PlatformViewPlugin
import io.flutter.embedding.android.FlutterActivityLaunchConfigs

class App : Application() {

    override fun onCreate() {
        super.onCreate()
        initFlutterBoost()
    }

    private fun initFlutterBoost() {
        val options = FlutterBoostSetupOptions.Builder().shellArgs(arrayOf("--mode=debug")).build()
        FlutterBoost.instance().setup(this, object : FlutterBoostDelegate {
            override fun pushNativeRoute(options: FlutterBoostRouteOptions) {

            }

            override fun pushFlutterRoute(options: FlutterBoostRouteOptions) {
                val intent = FlutterBoostActivity.CachedEngineIntentBuilder(MainActivity::class.java)
                    .destroyEngineWithActivity(false)
                    .uniqueId(options.uniqueId())
                    .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.opaque)
                    .url(options.pageName())
                    .urlParams(options.arguments())
                    .build(FlutterBoost.instance().currentActivity())

                FlutterBoost.instance().currentActivity().startActivity(intent)
            }
        }) {
            it.plugins.add(PlatformViewPlugin())
        }
    }
}